﻿using System;
using System.Collections;
using System.Runtime.InteropServices;

namespace Dialup_Tester
{
    public class Dial
    {
        // WININET.DLL stuff
        
        const int INTERNET_DIAL_UNATTENDED = 0x8000;
        const int INTERNET_AUTODIAL_FORCE_UNATTENDED = 2;

        //[DllImport("wininet.dll")] private extern static int InternetDial(IntPtr hwndParent, string lpszConnectoid, int dwFlags, out int lpdwConnection, int dwReserved);
        //[DllImport("wininet.dll")] private extern static int InternetAutoDial(IntPtr hwndParent, string lpszConnectoid, int dwFlags, out int lpdwConnection, int dwReserved);
        [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(ref int flags, int reservedValue);
        [DllImport("wininet.dll", SetLastError = true)] extern static int InternetHangUp(int Connection, int ReservedValue);

        public int connectionID = 0;

        public int Connect()
        {
            //return InternetDial(IntPtr.Zero, "Nursat", INTERNET_DIAL_UNATTENDED, out connectionID, 0);
            //return InternetDial(IntPtr.Zero, "Nursat", 8, out connectionID, 0);
            return 0;
        }

        public bool IsConnected()
        {
            const int INTERNET_CONNECTION_MODEM = 0x1;
            int connectionFlags = 0;
            
            InternetGetConnectedState(ref connectionFlags, 0);
            
            if ((connectionFlags & INTERNET_CONNECTION_MODEM) == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool HangUp()
        {
            if (InternetHangUp(connectionID, 0) == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
